<template>
  <div class="mod-config">
    <!-- 分成两部分 -->
    <el-row :gutter="20">
      <!--   部门   -->
      <el-col :span="4" :xs="24">
        <el-input
        v-model="deptName"
        placeholder="请输入部门名称"
        clearable
        size="small"
        style="margin-bottom: 20px"
        ></el-input>
        <el-tree
          :data="menuList"
          :props="menuListTreeProps"
          node-key="deptId"
          ref="menuListTree"
          @node-click="menuListTreeCurrentChangeHandle"
          :filter-node-method="filterDeptNode"
          :default-expand-all="true"
          :highlight-current="true"
          :check-on-click-node="true"
          :expand-on-click-node="false"
          style="background-color: #E9FFEB;">
        </el-tree>
      </el-col>
      <!--  用户数据    -->
      <el-col :span="20" :xs="24">
        <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
          <el-form-item>
            <el-input v-model="dataForm.idcard" placeholder="1:平板P  2:厢式X" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="getDataList()">查询</el-button>
            <el-button v-if="isAuth('PCguanli:line:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
            <el-button v-if="isAuth('PCguanli:line:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
          </el-form-item>
        </el-form>
        <el-table
          :data="dataList"
          border
          v-loading="dataListLoading"
          @selection-change="selectionChangeHandle"
          style="width: 100%;">
          <el-table-column
            type="selection"
            header-align="center"
            align="center"
            width="50">
          </el-table-column>
          <el-table-column
            prop="numberre"
            header-align="center"
            align="center"
            label="流水号">
          </el-table-column>
<!--          <el-table-column-->
<!--            prop="deptId"-->
<!--            header-align="center"-->
<!--            align="center"-->
<!--            label="部门编号">-->
<!--          </el-table-column>-->
          <el-table-column
            prop="carshop"
            header-align="center"
            align="center"
            label="所属车商">
          </el-table-column>
          <el-table-column
            prop="idrp"
            header-align="center"
            align="center"
            label="驻厂人员">
          </el-table-column>
          <el-table-column
            prop="idcard"
            header-align="center"
            align="center"
            label="司机身份证号"
            width="165">
          </el-table-column>
          <el-table-column
            prop="lineorder"
            header-align="center"
            align="center"
            label="车型">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.lineorder == 2">厢式X</el-tag>
              <el-tag v-if="scope.row.lineorder == 1" style="background-color: cadetblue; color: white">平板P</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="urgency"
            header-align="center"
            align="center"
            label="急单">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.urgency == 0">正常</el-tag>
              <el-tag v-if="scope.row.urgency == 1" style="background-color: red; color: white; font-size: medium">急单</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="queuePosition"
            header-align="center"
            align="center"
            label="排队号码">
          </el-table-column>
          <el-table-column
            prop="registertime"
            header-align="center"
            align="center"
            label="报道时间"
            width="170">
          </el-table-column>
          <el-table-column
            prop="registerstate"
            header-align="center"
            align="center"
            label="报道状态">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.registerstate == 1">已叫号状态</el-tag>
              <el-tag v-if="scope.row.registerstate == 2" style="background-color: #0BB2D4; color: white;">排队中</el-tag>
              <el-tag v-if="scope.row.registerstate == 3" style="background-color: #b3e19d; color: white;">已报到</el-tag>
              <el-tag v-if="scope.row.registerstate == 4" style="background-color: #6dbfff; color: white;">未报到</el-tag>
              <el-tag v-if="scope.row.registerstate == 5" style="background-color: #2D64B3; color: white;">已出厂</el-tag>
<!--              已叫号状态为第一次序1，排队中状态为第二次序2，已报到为第三次序3，未报到为第四次序4，已出厂为第5次序5-->
            </template>
          </el-table-column>
          <el-table-column
            fixed="right"
            header-align="center"
            align="center"
            width="150"
            label="操作">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.numberre)">修改</el-button>
              <el-button type="text" size="small" @click="deleteHandle(scope.row.numberre)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          :total="totalPage"
          layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
      </el-col>
    </el-row>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './line-add-or-update'
  import { treeDataTranslate } from '@/utils'
  export default {
    data () {
      return {
        dataForm: {
          lineorder: '',
          deptId: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        menuList: [],
        menuListTreeProps: {
          label: 'deptName',
          children: 'children'
        },
        deptName: ''
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    watch: {
      deptName (value) {
        this.$refs.menuListTree.filter(value)
      }
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.$http({
          url: this.$http.adornUrl('/PCguanli/linedept/list'),
          method: 'get',
          params: this.$http.adornParams({
            'key': this.dataForm.key
          })
        }).then(({data}) => {
          this.menuList = treeDataTranslate(data, 'deptId')
        })
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/PCguanli/line/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'lineorder': this.dataForm.idcard,
            'deptId': this.dataForm.deptId
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            // 计算排队号码
            const startIndex = (this.pageIndex - 1) * this.pageSize
            data.page.list.forEach((item, index) => {
              item.queuePosition = startIndex + index + 1
            })
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.numberre
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/PCguanli/line/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      // 部门点击事件
      menuListTreeCurrentChangeHandle (data, node) {
        // this.dataForm.parentId = data.deptId
        // this.dataForm.parentName = data.deptName
        let checkedKeys = this.$refs.menuListTree.getCheckedKeys()
        let deptIdStr = ''
        for (let i = 0; i < checkedKeys.length; i++) {
          deptIdStr += checkedKeys[i] + ','
        }
        this.dataForm.deptId = deptIdStr
        this.getDataList()  // 重新获取数据
      },
      filterDeptNode (value, data, node) {
        // 节点过滤
        if (!value) return true
        return data.deptName.indexOf(value) !== -1
      }
    }
  }
</script>
